package com.swj.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * write something please~
 *
 * @author huangrz
 * @since 2021/11/8
 **/
@Configuration
public class RedisConfiguration {

    /**
     * value会自动通过jackson序列化
     *
     * @param redisConnectionFactory Redis链接工厂实例，负责处理链接池
     * @return RedisTemplate<String, Object>
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        template.setConnectionFactory(redisConnectionFactory);
        template.setValueSerializer(RedisSerializer.json());
        template.setHashValueSerializer(RedisSerializer.json());
        return template;
    }

    /**
     * 一个直接操作value的{@link ValueOperations}对象
     *
     * @param redisTemplate 上面注入的 RedisTemplate<String, Object> 实例
     * @return 返回一个直接操作value的{@link ValueOperations}对象
     */
    @Bean
    public ValueOperations<String, Object> valueOperations(RedisTemplate<String, Object> redisTemplate) {
        return redisTemplate.opsForValue();
    }

}
